package com.apache.bid.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.common.Constant;
import com.apache.bid.entity.BidItemPrice;
import com.apache.bid.manager.BidItemPriceManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;

public class BidItemPriceManagerImpl implements BidItemPriceManager {

	protected IDao bidItemPriceDao;
	protected final String entityName = "com.apache.bid.entity.BidItemPrice";

	public void setBidItemPriceDao(IDao bidItemPriceDao) {
		this.bidItemPriceDao = bidItemPriceDao;
	}

	public String saveInfo(ParamsVo<BidItemPrice> vo) throws BusinessException {
		BidItemPrice bidItemPrice = vo.getObj();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="bidItemPrice_"+infoId;
		if (Constant.getInstance().ifWriteDatabase()) {

			//设置缺省值
			bidItemPrice.setBidTimes(Validator.getDefaultStr(bidItemPrice.getBidTimes(), "0"));//报价次数
			bidItemPrice.setDelayTimes(Validator.getDefaultStr(bidItemPrice.getBidTimes(), "0"));//第几次延牌
			bidItemPrice.setBidNum(Validator.getDefaultStr(bidItemPrice.getBidNum(), "0"));//报价数量（每次报价更新）
			bidItemPrice.setDelayEndFlag(Validator.getDefaultStr(bidItemPrice.getDelayEndFlag(), "F"));//延牌是否结束 T 表示结束
			bidItemPrice.setIfAddPrice(Validator.getDefaultStr(bidItemPrice.getIfAddPrice(), "F"));////是否已加价（T/F）
			bidItemPrice.setIfPrioFlag(Validator.getDefaultStr(bidItemPrice.getIfPrioFlag(), "F"));//是否已经行驶优先权（T/F）（场内：自己点，场外：交易所线下询问完后代替点）
			bidItemPrice.setLastValue(Validator.getDefaultStr(bidItemPrice.getLastValue(), "0"));//最高报价
			bidItemPrice.setBidTimes(Validator.getDefaultStr(bidItemPrice.getBidTimes(), "0"));//初始报价次数=0

			MethodParam param = new MethodParam("BidItemPrice", cacheKey, "", entityName);
			param.setVaule(bidItemPrice);
			if (bidItemPriceDao.insert(param)) {
				return bidItemPrice.getItemId();
			}
		} else {
			///////////////需要处理缓存结果//////////////////
			return bidItemPrice.getItemId();
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidItemPrice> vo) throws BusinessException {
		BidItemPrice bidItemPrice = vo.getObj();
		if (Validator.isNotNull(bidItemPrice.getItemId())) {
			String cacheKey = "";
			//String cacheKey="bidItemPrice_"+bidItemPrice.getItemId();
			//if (Constant.getInstance().ifWriteDatabase()) {
			MethodParam param = new MethodParam("BidItemPrice", cacheKey, "", entityName);
			param.setVaule(bidItemPrice);
			return bidItemPriceDao.edit(param);
			//			} else {
			//				///////////////需要处理缓存结果//////////////////
			//				return true;
			//			}
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidItemPrice> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="bidItemPrice_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		//if (Constant.getInstance().ifWriteDatabase()) {
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		if ("false".equals(mark)) {//逻辑删除
			param.setInfoId(infoId);
			BidItemPrice info = (BidItemPrice) bidItemPriceDao.selectById(param);
			if (Validator.isEmpty(info)) {
				return false;
			}
			param.setVaule(info);//此处需要先将状态值赋值为删除状态
			param.setKey("BidItemPrice");
			return bidItemPriceDao.edit(param);
		} else {
			param.setParams("itemId", infoId);
			param.setDelete(true);
			return bidItemPriceDao.delete(param);
		}
		//		} else {
		//			///////////////需要处理缓存结果//////////////////
		//			if ("false".equals(mark)) {//逻辑删除
		//			} else {
		//			}
		//			return true;
		//		}
	}

	public Object getInfoById(ParamsVo<BidItemPrice> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidItemPrice_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return bidItemPriceDao.selectById(param);
	}

	public Object execute(ParamsVo<BidItemPrice> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<BidItemPrice> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		param.setOrderby(" close_time desc");
		Page page = bidItemPriceDao.pageSelect(param);
		return page;
	}

	public List<BidItemPrice> getList(ParamsVo<BidItemPrice> vo) {
		MethodParam param = setMethodParams(vo, 2);
		param.setOrderby(" close_time desc");
		return bidItemPriceDao.select(param);
	}

	public long countInfo(ParamsVo<BidItemPrice> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidItemPriceDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidItemPrice> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidItemPriceDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		BidItemPrice bidItemPrice = vo.getObj();
		param.setResultCache(false);
		String sessionId = String.valueOf(vo.getParams("sessionId"));
		if (Validator.isNotNull(sessionId)) {
			param.setParams("sessionId", sessionId);
		}
		//if (Validator.isEmpty(bidItemPrice)) {
		param.setParams(vo.getParams());
		//	return param;
		//}
		if (!Validator.isEmpty(bidItemPrice)) {
			if (Validator.isNotNull(bidItemPrice.getOpenTime())) {
				sb.append(" and openTime = :openTime");
				param.setParams("openTime", bidItemPrice.getOpenTime());
			}
			if (Validator.isNotNull(bidItemPrice.getFirstValue())) {
				sb.append(" and firstValue = :firstValue");
				param.setParams("firstValue", bidItemPrice.getFirstValue());
			}
			if (Validator.isNotNull(bidItemPrice.getFirstBidTime())) {
				sb.append(" and firstBidTime = :firstBidTime");
				param.setParams("firstBidTime", bidItemPrice.getFirstBidTime());
			}
			if (Validator.isNotNull(bidItemPrice.getLastValue())) {
				sb.append(" and lastValue = :lastValue");
				param.setParams("lastValue", bidItemPrice.getLastValue());
			}
			if (Validator.isNotNull(bidItemPrice.getAmount())) {
				sb.append(" and amount = :amount");
				param.setParams("amount", bidItemPrice.getAmount());
			}
			if (Validator.isNotNull(bidItemPrice.getUserId())) {
				sb.append(" and userId = :userId");
				param.setParams("userId", bidItemPrice.getUserId());
			}
			if (Validator.isNotNull(bidItemPrice.getUserName())) {
				sb.append(" and userName = :userName");
				param.setParams("userName", bidItemPrice.getUserName());
			}
			if (Validator.isNotNull(bidItemPrice.getLastRightLevel())) {
				sb.append(" and lastRightLevel = :lastRightLevel");
				param.setParams("lastRightLevel", bidItemPrice.getLastRightLevel());
			}
			if (Validator.isNotNull(bidItemPrice.getBidTimes())) {
				sb.append(" and bidTimes = :bidTimes");
				param.setParams("bidTimes", bidItemPrice.getBidTimes());
			}
			if (Validator.isNotNull(bidItemPrice.getCloseTime())) {
				sb.append(" and closeTime = :closeTime");
				param.setParams("closeTime", bidItemPrice.getCloseTime());
			}
			if (Validator.isNotNull(bidItemPrice.getLastBidTime())) {
				sb.append(" and lastBidTime = :lastBidTime");
				param.setParams("lastBidTime", bidItemPrice.getLastBidTime());
			}
			if (Validator.isNotNull(bidItemPrice.getPauseTime())) {
				sb.append(" and pauseTime = :pauseTime");
				param.setParams("pauseTime", bidItemPrice.getPauseTime());
			}
			if (Validator.isNotNull(bidItemPrice.getDelayTimes())) {
				sb.append(" and delayTimes = :delayTimes");
				param.setParams("delayTimes", bidItemPrice.getDelayTimes());
			}
			if (Validator.isNotNull(bidItemPrice.getDelayEndFlag())) {
				sb.append(" and delayEndFlag = :delayEndFlag");
				param.setParams("delayEndFlag", bidItemPrice.getDelayEndFlag());
			}
			if (Validator.isNotNull(bidItemPrice.getIfAddPrice())) {
				sb.append(" and ifAddPrice = :ifAddPrice");
				param.setParams("ifAddPrice", bidItemPrice.getIfAddPrice());
			}
			if (Validator.isNotNull(bidItemPrice.getIfPrioFlag())) {
				sb.append(" and ifPrioFlag = :ifPrioFlag");
				param.setParams("ifPrioFlag", bidItemPrice.getIfPrioFlag());
			}
			if (Validator.isNotNull(bidItemPrice.getBidNum())) {
				sb.append(" and bidNum = :bidNum");
				param.setParams("bidNum", bidItemPrice.getBidNum());
			}
			if (Validator.isNotNull(bidItemPrice.getWeight())) {
				sb.append(" and weight = :weight");
				param.setParams("weight", bidItemPrice.getWeight());
			}
			if (Validator.isNotNull(bidItemPrice.getWeight())) {
				sb.append(" and weight = :weight");
				param.setParams("weight", bidItemPrice.getWeight());
			}
		}
		param.setSqlStr(sb.toString());
		return param;
	}
}
